<?php
namespace app\admin\controller;

class Goods extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','a.title','like',"%{$d['keyword']}%"]; }
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            if($d['cat_id']!=''){
                $ids = m('GoodsCat','c')->getAllchildCat($d['cat_id']);
                if($ids){
                    $ids[] = $d['cat_id'];
                }else{
                    $ids = [$d['cat_id']];
                }
                $map[] = ['and','a.cat_id','in',$ids];
            }
            $w  = where($map);
            $rs = $this->db->query("SELECT a.*,b.cat_title ptitle FROM goods a LEFT JOIN goods_cat b ON a.cat_id = b.id  $w[0] ORDER BY a.sort DESC,a.id DESC".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status'=>function($v){return c("type.status.$v");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $cat = m('GoodsCat','c')->allSubCat();
        return $this->fetch(['cat'=>$cat]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'cat_id'=>'require',
                'title' => "require",
                'money' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            unset($d['file']);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $id = $this->db->insert('goods',$d);
            m('Attachment','xcom')->imgSure('goods',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $cat = m('GoodsCat','c')->allSubCat();
        return $this->fetch(['cat'=>$cat]);
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'cat_id'=>'require',
                'title' => "require",
                'money' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            unset($d['file']);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $id = $this->db->update('goods',$d);
            m('Attachment','xcom')->imgSure('goods',$id,$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $cat = m('GoodsCat','c')->allSubCat();
        $id = $this->get['id'];
        $info = $this->db->query("select * from goods where id=?",[$id],1);
        $info['pics'] = explode(',',$info['pics']);
        return $this->fetch(['cat'=>$cat,'info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('goods',$d['id']);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('goods',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }
    //修改上下架
    public function updateStatus(){
      if(is_post()){
            $d = $this->post;
            $status = $this->get('status');
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->update('goods',['status'=>$status],['id=?',[$id]]);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','reload');
            return suc();
      }
    }
}
